#include <iostream>
#include <boost/program_options.hpp>
#include <string>
#include <cmath>
#include "get_arg.h"


// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp

#define DEBUG_LEVEL 6
#include <debug_func.h>



// my functions
#include <open_stream_name_with_subscript.h>
#include <print_error.h>
#include <program_options_helper.h>
#include <matrix_formats.h>

using std::string;
using std::cerr;
using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	char input_format, output_format;

	args.optional_args.add_options()
	("input_format,i", po::value(&input_format)
										->default_value('p')
										->set_name("FORMAT"),
									"Input FORMAT is binary ('b'), pairs ('p'), "
									"square ('s') or lower triangular ('l') .")
	("matrix_output,m",				"Append the original matrix to each resulting tree "
									"in the output")
	("output_format,o", po::value(&output_format)
										->default_value('p')
										->set_name("FORMAT"),
									"Only relevant if --matrix_output is specified.\1 "
									"Output matrix FORMAT is binary ('b'), pairs ('p'), "
									"square ('s') or lower triangular ('l') .")
	("header,H",					"Prefix each tree with a header line\1"
									"(matrix name prefixed with '>').");
	args.add_std_options();
	po::options_description all_args("");
	all_args.add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.1\n"
			"\tHistory: \n\n"
			"\tv 1.0\1"
			"Initial version\n"
			"\tv 1.1\1"
			"Fixed nasty bug where top-most clusters were joined by zero length branches.\n"
			);
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =
				"Takes distances from STDIN and resolves (crudely) the phylogenetic tree "
				"of one or more group of relationships.\n"
				"Uses single linkage clustering and a modified version of UPGMA that "
				"takes null relationships into account.\n"
				"Trees are written to STDOUT";
		print_usage(cerr, string(*argv), "[OPTIONS] < MATRIX_FILE > TREE_OUTPUT",
							all_args, exe_description, 80);
		return false;
	}

	args.header					= vm.count("header");
	args.add_matrix_to_output	= vm.count("matrix_output");


	select_matrix_file_format(input_format, args.input_format, "input format");
	select_matrix_file_format(output_format, args.output_format, "output format");


	args.open_err_log();
	return true;
}


